package com.cn.por.auto.factory;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.type.JdbcType;

import com.cn.por.auto.dto.DataModel;
import com.cn.por.auto.dto.EntityDataModel;
import com.cn.por.auto.dto.MapperRelation;
import com.cn.por.auto.dto.ModelParam;
import com.cn.por.auto.entity.TableDetiles;
import com.cn.por.util.freemarker.StringFormaterUtil;

public class EntityDataModelFactory implements DataModelFactory{
	
	private EntityDataModel entityDataModel;
	public EntityDataModelFactory() {}
	@Override
	public void setProperties(ModelParam modelParam) {
		entityDataModel = new EntityDataModel();
		/**
		 * entityName
		 */
		String entityName = StringFormaterUtil.columnToProperty(modelParam.getTableName());
		/**
		 * 包名
		 */
		entityDataModel.setPackageName(
				modelParam.getBasePackage()
						+ ".entity"
						);
		/**
		 * 类名称
		 */
		entityDataModel.setClassName(entityName);
		/**
		 * BaseResultMap 建立数据库字段与java对象映射关系
		 */
		List<MapperRelation> mapperRelationList = new ArrayList<MapperRelation>();
		for (TableDetiles column : modelParam.getTableColumn()) {
			MapperRelation mapperRe = new MapperRelation();
			mapperRe.setColumn(column.getColumnName().toUpperCase());
			mapperRe.setProperty(
					StringFormaterUtil.lowerCaptureName(StringFormaterUtil.columnToProperty(column.getColumnName())));
			for(JdbcType jdbc : JdbcType.values()) {
				if((column.getDataType().equals("int")?"integer":column.getDataType()).toLowerCase().equals(jdbc.name().toLowerCase())) {
					mapperRe.setPropertyType(StringFormaterUtil.typeTransformer(jdbc));
					break;
				}
			}
			mapperRelationList.add(mapperRe);
		}
		entityDataModel.setMapperRelations(mapperRelationList);
		entityDataModel.setFileName(modelParam.getFileName());
		entityDataModel.setFtlName(modelParam.getFtlName());
		entityDataModel.setFilePath(modelParam.getFilePath());
		entityDataModel.setFolder(entityDataModel.getPackageName().replace(".", "/"));
	}

	@Override
	public DataModel getDataModel() {
		return this.entityDataModel;
	}
	
	

}
